// packageF/storeManagement/components/baseInfo_popup/goods_category_pop/goods_category_pop.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    categoryList:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    sectionOptions: [],
    objectMultiArray: [],
    multiIndex: [0, 0, 0],
  },
  lifetimes:{
    attached(){
      this.data.sectionOptions = this.formatList(this.data.categoryList);
      this.goodsHandlefunction();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showPopup(){
      this.setData({
        show:!this.data.show
      });
    },
    formatList (arr) {
      let formatData = [];
      arr.forEach((item, index) => {
        let obj = {};
        let {id, name, parent_id} = item;
        obj = {
          id,
          parent_id,
          name,
        };
        if (item.has_many_children && item.has_many_children.length > 0) {
          obj.children = this.formatList(item.has_many_children);
        }
        formatData[index] = obj;
      });
      return formatData;
    },
    commodityBtn(e) {
      let column = e.detail.column;
      let val = e.detail.value;
      const data = {
        multiArray: this.data.objectMultiArray
      };
      this.setData({
        ['multiIndex[' + column + ']']: val
      });
      switch (column) {
      case 0:
        this.selecommodity_0(val);
        break;
      case 1:
        this.selecommodity_1(this.data.multiIndex[0], val);
        break;
      case 2:
        break;
      }
      this.setData(data);
    },
    commodityconfirm(e) {
      // var i = e.detail.value[0];
      // if (this.data.list[i].childrens.length == 0) {
      //   wx.showToast({
      //     title: '至少要选择二级分类',
      //     icon: 'none',
      //     duration: 2000
      //   });
      //   return;
      // }
      let sectionOptions = this.data.sectionOptions;
      let indexArray = e.detail.value;
      let index_0 = indexArray[0];
      let index_1 = indexArray[1];
      let index_2 = indexArray[2];
      let idArray = [];
      let strArray = [];
      if (sectionOptions.length > 0) {
        idArray.push(sectionOptions[index_0].id);
        strArray.push(sectionOptions[index_0].name);
        if (sectionOptions[index_0].children && sectionOptions[index_0].children.length > 0) {
          idArray.push(sectionOptions[index_0].children[index_1].id);
          strArray.push(sectionOptions[index_0].children[index_1].name);
          if (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length >
            0) {
            idArray.push(sectionOptions[index_0].children[index_1].children[index_2].id);
            strArray.push(sectionOptions[index_0].children[index_1].children[index_2].name);
          }
        }
      }
      let manyGoodsCategory = {};
      if (idArray.length >= 1) {
        manyGoodsCategory.parentid = idArray[0];
        manyGoodsCategory.parent_name = strArray[0];
      }
      if (idArray.length >= 2) {
        manyGoodsCategory.childid = idArray[1];
        manyGoodsCategory.child_name = strArray[1];
      }
      if (idArray.length >= 3) {
        manyGoodsCategory.thirdid = idArray[2];
        manyGoodsCategory.third_name = strArray[2];
      }
      this.triggerEvent("confirmType",{manyGoodsCategory});
      // this.setData({
      //   regional: strArray.join('/'),
      //   selectedOptions3: idArray
      // });
    },
    goodsHandlefunction() {
      let sectionOptions = this.data.sectionOptions;
      let arr_1 = [];
      let arr_2 = [];
      let arr_3 = [];
      for (let i = 0; i < sectionOptions.length; i++) {
        arr_1.push({
          id: sectionOptions[i].id,
          name: sectionOptions[i].name

        });
      }
      if (sectionOptions[0].children && sectionOptions[0].children.length > 0) {
        for (let k = 0; k < sectionOptions[0].children.length; k++) {
          arr_2.push({
            id: sectionOptions[0].children[k].id,
            name: sectionOptions[0].children[k].name
          });
        }
      }
      if ((sectionOptions[0].children && sectionOptions[0].children.length > 0) && (sectionOptions[0].children[0].children &&
        sectionOptions[0].children[0].children.length > 0)) {
        for (let j = 0; j < sectionOptions[0].children[0].children.length; j++) {
          arr_3.push({
            id: sectionOptions[0].children[0].children[j].id,
            name: sectionOptions[0].children[0].children[j].name
          });
        }
      }
      this.setData({
        'objectMultiArray[0]': arr_1,
        'objectMultiArray[1]': arr_2,
        'objectMultiArray[2]': arr_3
      });
    },
    selecommodity_0(index) {
      let sectionOptions = this.data.sectionOptions;
      let arr_2 = [];
      let arr_3 = [];
      if (sectionOptions[index].children && sectionOptions[index].children.length > 0) {
        for (let k = 0; k < sectionOptions[index].children.length; k++) {
          arr_2.push({
            id: sectionOptions[index].children[k].id,
            name: sectionOptions[index].children[k].name
          });
        }
      }
      if ((sectionOptions[index].children && sectionOptions[index].children.length > 0) && (sectionOptions[index].children[0].children && sectionOptions[index].children[0].children.length > 0)) {
        for (let j = 0; j < sectionOptions[index].children[0].children.length; j++) {
          arr_3.push({
            id: sectionOptions[index].children[0].children[j].id,
            name: sectionOptions[index].children[0].children[j].name
          });
        }
      }
      this.setData({
        'objectMultiArray[1]': arr_2,
        'objectMultiArray[2]': arr_3,
        'multiIndex[1]': 0,
        'multiIndex[2]': 0
      });
    },
    selecommodity_1(index_0, index_1) {
      let sectionOptions = this.data.sectionOptions;
      let arr_3 = [];
      if ((sectionOptions[index_0].children && sectionOptions[index_0].children.length > 0) && (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length > 0)) {
        for (let j = 0; j < sectionOptions[index_0].children[index_1].children.length; j++) {
          arr_3.push({
            id: sectionOptions[index_0].children[index_1].children[j].id,
            name: sectionOptions[index_0].children[index_1].children[j].name
          });
        }
      }
      this.setData({
        'objectMultiArray[2]': arr_3,
        'multiIndex[2]': 0
      });
    },
  }
});
